package logic

import (
	"context"

	"group/internal/svc"
	"group/pb/group"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetApplyListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetApplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApplyListLogic {
	return &GetApplyListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetApplyList 群聊申请列表
func (l *GetApplyListLogic) GetApplyList(in *group.ApplyListRequest) (*group.ApplyListResponse, error) {
	total, err := l.svcCtx.GroupApplyExtendModel.Count(l.ctx, in.GroupId)
	if err != nil {
		return nil, err
	}
	list, err := l.svcCtx.GroupApplyExtendModel.FindList(l.ctx, in.GroupId, in.Offset, in.Limit, in.Status)
	if err != nil {
		return nil, err
	}

	var data []*group.ApplyInfo
	for _, item := range list {
		data = append(data, &group.ApplyInfo{
			Id:            item.Id,
			UserId:        item.UserId,
			GroupId:       item.GroupId,
			Message:       item.Message,
			Status:        item.Status,
			HandlerUserId: item.HandlerUserId,
			HandlerTime:   item.HandleTime.Time.Unix(),
			CreateAt:      item.CreatedAt.Unix(),
			UpdateAt:      item.UpdatedAt.Unix(),
		})
	}
	return &group.ApplyListResponse{Total: total, Data: data}, nil
}
